//package com.nuoniu.sibanyun.interceptor.handler;
//
//
//import javax.servlet.http.Cookie;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//
//import com.nuoniu.sibanyun.config.JwtUtils;
//import com.nuoniu.sibanyun.service.redis.RedisService;
//import com.nuoniu.sibanyun.userThread.UserInfo;
//import com.nuoniu.sibanyun.userThread.UserThreadLocal;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//import org.springframework.util.StringUtils;
//import org.springframework.web.servlet.HandlerInterceptor;
//import org.springframework.web.servlet.ModelAndView;
//import com.fasterxml.jackson.databind.ObjectMapper;
///**
// * @ClassName UserInterceptor
// * @Description TODO
// * @Authpr baiqiang
// * @Date 2020/5/29 11:26
// **/
//
////handler处理器 作用:真正执行COntroller-Servic-Mapper
//@Component
//public class UserInterceptor implements HandlerInterceptor{
//
//    @Autowired
//    private RedisService redisService;
//    private static ObjectMapper objectMapper = new ObjectMapper();
//
//    //在执行COntroller方法之前执行
//    /**
//     * boolean 表示是否放行
//     * 	 true:放行 用户可以跳转页面
//     * 	 false:拦截  之后给定重定向路径
//     */
//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
//            throws Exception {
//        String token = JwtUtils.getToken(request);
//
//        if(!StringUtils.isEmpty(token)){
//            //检测缓存中是否有该数据
//            String userJSON = redisService.get(token) != null ? redisService.get(token).toString() : "";
//            if(!StringUtils.isEmpty(userJSON)){
//                //将userJSON转化为User对象
//                UserInfo user = objectMapper.readValue(userJSON, UserInfo.class);
//                //思路.使用Session共享
//                UserThreadLocal.set(user);
//                //用户已经登陆 放行请求
//                return true;
//            }
//        }
//        return false;
//    }
//
//    //执行完业务逻辑后拦截
//    @Override
//    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
//                           ModelAndView modelAndView) throws Exception {
//        // TODO Auto-generated method stub
//
//    }
//
//    //返回页面之前拦截
//    @Override
//    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
//            throws Exception {
//
//        //将ThreadLocal数据清空
//        UserThreadLocal.remove();
//    }
//}
